# -*- coding:utf-8 -*-
import time
import urllib.request
import urllib,os
import json
import zlib
import urllib.parse
from selenium import webdriver
import sys
class test4:
    def __init__(self):
        initPath=os.getcwd()+"//cache//init.lock"
        if os.path.exists(initPath):#判断是不是需要重新获取数据,如果存在init.lock文件，就获取最新数据
            data=self.get_data()
            if data!=None:
                for dataName in data:
                    self.save_data_to_file(data.get(dataName),dataName)
                    self.save_data_to_file(self.split_data(data.get(dataName)),dataName+"List")
            os.remove(initPath)
        # self.driver=webdriver.PhantomJS(executable_path="D:\\soft\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
    def save_data_to_file(self,data,fileName):
        filePath=os.getcwd()+"//cache//"+fileName+".cache"
        if not os.path.exists(filePath):
            with open(filePath,'a+',encoding='utf-8') as bp:
                bp.write(json.dumps(data,ensure_ascii=False))
        else: 
            with open(filePath,'w+',encoding='utf-8') as bp:
                bp.write(json.dumps(data,ensure_ascii=False))
    def get_cache_by_fileName(self,fileName):
        filePath=os.getcwd()+"//cache//"+fileName+".cache"
        if not os.path.exists(filePath):
            return None
        else: 
            with open(filePath,'r+',encoding='utf-8') as fp:
                a=fp.read()
                data=json.loads(a)
            return data
    def get_data(self):
        result={}
        timestr=time.strftime("%Y%m%d",time.localtime(time.time()))
        req_url="http://sou.zhaopin.com/assets/javascript/basedata.js?v=%s"%timestr
        req=urllib.request.Request(req_url)
        try:
            req_data=urllib.request.urlopen(req)
        except:
            return None
        responese_data=req_data.read()
        data=responese_data.decode("utf-8")
        data1=data.split(";")
        data3=""
        for data2 in data1:
            if "dIndustry" in data2:
                data3=data2.replace("var dIndustry=","")
                result.setdefault("dIndustry",data3)
            elif "dJobtype" in data2:
                data3=data2.replace("\nvar dJobtype=","")
                result.setdefault("dJobtype",data3)
            elif "dSubjobtype" in data2:
                data3=data2.replace("\nvar dSubjobtype = ","")
                if "dSubjobtype" in data3:
                    data3=data2.replace("\nvar dSubjobtype=","")
                result.setdefault("dSubjobtype",data3)
            elif "jobtypeClass" in data2:
                data3=data2.replace("\nvar jobtypeClass=","")
                result.setdefault("jobtypeClass",data3)
            elif "dCity" in data2:
                data3=data2.replace("\nvar dCity=","")
                result.setdefault("dCity",data3)
        return result
    def split_data(self,data):
        a=data.split("@")
        result_1={}
        result={}
        zylb=""
        for i in a:
            if i!="" and i!="'":
                b=i.split("|")
                if b[2]==zylb:
                    result_1.setdefault(b[1],b[0])
                    if i==a[-2]:
                        result.setdefault(zylb,result_1)     
                else:
                    result.setdefault(zylb,result_1)
                    zylb=b[2]
                    result_1={}
                    result_1.setdefault(b[1],b[0])
        return result
    def get_jobValueList_by_jobType(self,dSubjobtype):#获取所有职业类别对应的搜索编码
        result={}
        jobtypelist=self.split_data(dSubjobtype)
        for bj in jobtypelist:
            if jobtypelist.get(bj)!={}:
                for jobname in jobtypelist.get(bj):
                    value="bj=%(bj)s&sj=%(sj)s" % dict (
                        bj=bj,
                        sj=jobtypelist.get(bj).get(jobname)
                    )
                    result.setdefault(jobname,value)
        return result
    def get_dIndustryValueList_by_dIndustry(self,dIndustry):
        result={}
        dIndustrylist=self.split_data(dIndustry)
        for type in dIndustrylist:
            if dIndustrylist.get(type)!={}:
                for dIndustryName in dIndustrylist.get(type):
                    value="in=%s"%dIndustrylist.get(type).get(dIndustryName)
                    result.setdefault(dIndustryName,value)
        return result
    def get_job_data_by_select(self,url):
        driver=webdriver.PhantomJS(executable_path="D:\\soft\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
        # driver=self.driver
        driver.get(url)
        result=[]
        isfrist=True
        for i in driver.find_element_by_id("newlist_list_content_table").find_elements_by_tag_name("tr"):
            if i.text!="":
                result1=[]
                if isfrist:
                    isfrist=False
                    continue
                result1=i.text.split(" ")
                adress=i.find_element_by_tag_name("a").get_attribute("href")
                result1.append(adress)
                result.append(result1)
        driver.quit()
        return result
    def run(self):
        url=[]
        dCityList=self.get_cache_by_fileName("dCityList")
        dSubjobtype=self.get_cache_by_fileName("dSubjobtype")
        b=self.get_jobValueList_by_jobType(dSubjobtype)
        for i in dCityList:
            url1=""
            if dCityList.get(i)=={}:
                continue
            for j in dCityList.get(i):
                for m in b:
                    url1="http://sou.zhaopin.com/jobs/searchresult.ashx?%(zw)s&jl=%(address)s&p=1&isadv=0" % dict(
                        zw=b.get(m),
                        address=j
                    )
                    url.append(url1)
                    # print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
                    # c=self.get_job_data_by_select(url1)
                    # print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
                    # print(str(len(c)))
        print(str(sys.getsizeof(url)))            
        self.save_data_to_file(url,"url")

def main():
    test=test4()
    # a=test.get_dIndustryValueList_by_dIndustry(test.get_cache_by_fileName("dIndustry"))
    # b=1
    # test.get_job_data_by_select("http://sou.zhaopin.com/jobs/searchresult.ashx?jl=选择地区&p=1&isadv=0")
    test.run()


if __name__ == '__main__':
    main()